<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>分类管理</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link type="text/css" rel="stylesheet" href="/easybuy/css/ProductCategoryList.css" />
	<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
	<script type="text/javascript">
	$(function() {
		pc();
		if($("#pageIndex").val()!=""){
			pageIndex = Number($("#pageIndex").val());
		} else if($("#pageCount").val()!=""){
			pageIndex = pageCount;
		}
		showCategoryList(pageIndex);
		parentName(1);
		$("#type").change(function(){
			parentName($("#type").val());
			if($("#type").val()==1){
				$("#tu").css("display","inline-block");
				$("#file").css("display","inline-block");
			}else{
				$("#tu").css("display","none");
				$("#file").css("display","none");
			}
		});
		$("#add").submit(function() {
			var name = $("#name").val();
			if (name == "") {
				alert("分类名称不能为空！");
				return false;
			}
			if($("#type").val()==1){
				var file=$("#file").val();
				if(file == ""){
					alert("您未选择任何文件！");
					return false;
				}
			}
		});
		$("#span").click(function(){
			$("#addCategory").hide();
			$("#productList").show();
			$("#paging").show();
		});
	});
	function add() {
		$("#addCategory").show();
		$("#productList").hide();
		$("#updateCategory").hide();
		$("#paging").hide();
	}
	function parentName(type){
		$.ajax({
			"url" : "category",
			"type" : "post",
			"data" : "opr=parentName&type="+type,
			"dataType" : "json",
			"success" : function(data) {
				$("#parentType").empty();
				if(type==1){
					$("#parentType").append("<option value='0'>无</option>");
				}else{
					for(var i=0;i<data.length;i++){
						$("#parentType").append("<option value='"+data[i].id+"'>"+data[i].parentName+"</option>");						
					}
				}
			}
		});
	};
	var pageIndex = 1;
	var pageCount = 0;
	//查询总页数
	function pc() {
		$.ajax({
			"url" : "category",
			"type" : "post",
			async:false,
			"data" : "opr=showCategoryList&child=pc",
			"dataType" : "text",
			"success" : function(data) {
				pageCount = Number(data);
				paging();
			}
		});
	}
	//查询商品列表
	function showCategoryList(pi) {
		pageIndex = pi;
		$.ajax({
			"url" : "category",
			"type" : "post",
			"data" : "opr=showCategoryList&pageIndex=" + pi,
			"dataType" : "json",
			"success" : function(data) {
				$("#pl").empty();
				$("#pl").append("<tr><td>选择</td><td>分类名称</td><td>分类级别</td><td>父级分类</td><td>图标</td><td colspan='2'>操作</td></tr>");
				for ( var i = 0; i < data.length; i++) {
					var type = "";
					if (data[i].type == 1) {
						type = "一级菜单";
					} else if (data[i].type == 2) {
						type = "二级菜单";
					} else if (data[i].type == 3) {
						type = "三级菜单";
					}
					var parentName = "";
					if (data[i].parentName == null) {
						parentName = "无";
					} else {
						parentName = data[i].parentName;
					}
					if(data[i].icon==null){
						icon="无";
					}else{
						icon="<img id='icon' src=images/"+data[i].icon+"></img>";
					}
					var str = "<tr><td><input type='radio' name='radio'/></td><td>"+ data[i].name+ "</td><td>"+ type+ "</td><td>"+ parentName+ "</td><td>"+icon+"</td><td><a href='javascript:del("+ data[i].id +","+pageIndex +")'>删除</a></td><td><a href='javascript:toupdate("+ data[i].id +","+data[i].type+")'>修改</a></td></tr>";
					$("#pl").append(str);
				}
			}
		});
		paging();
	}
	function toupdate(id,type){
		location.href="category?opr=toupdate&id="+id+"&type="+type+"&pageIndex="+pageIndex;
	}
	function del(id,pageIndex) {
		if (confirm("确定要删除吗？")) {
			$.ajax({
				"url" : "category",
				"type" : "post",
				async:false,
				"data" : "opr=delCategoryList&id="+id,
				"dataType" : "text",
				success:function(data){
					if(data>0){
						alert("删除成功！");
					}else if(data==-2){
						alert("该分类下还有子分类，不能删除！");
					}else if(data==-1){
						alert("该分类下还有商品，不能删除！");
					}else{
						alert("删除失败！");
					}
					pc();
					if(pageIndex>pageCount){
						showCategoryList(pageIndex-1);
					} else {
						showCategoryList(pageIndex);
					}
				}
			});
		}	
	}
	function pageNum(){
		var str = "";
		if(pageCount < 6){
			for(var i=1;i<=pageCount;i++){
				str += "<a href='javascript:showCategoryList("+i+")'>"+i+"</a>";
			}
		} else {
		for(var i=1;i<=pageCount;i++){
			if(pageIndex == 1){
				if(i>3 && i< pageCount-1){
					continue;
				}
				if(i == pageCount-1){
					str += "...";
				}
				str += "<a href='javascript:showCategoryList("+i+")'>"+i+"</a>";
			} else if (pageIndex <= pageCount - 3) {
				if(pageIndex >= (i-1) && pageIndex<=(i+1)){
					str += "<a href='javascript:showCategoryList("+i+")'>"+i+"</a>";
				}
				if(i == pageCount -1){
					str += "...";
				}
				if(i >= pageCount-1){
					str += "<a href='javascript:showCategoryList("+i+")'>"+i+"</a>";
				}
			} else if (pageIndex == pageCount - 2){
				if(i > pageIndex-3 && i <= pageIndex){
					str += "<a href='javascript:showCategoryList("+i+")'>"+i+"</a>";
				}
				if(i == pageIndex){
					str += "...";
				}
				if(i >pageCount -2){
					str += "<a href='javascript:showCategoryList("+i+")'>"+i+"</a>";
				}
			} else if (pageIndex >= pageCount - 3){
				if(i<=3){
					str += "<a href='javascript:showCategoryList("+i+")'>"+i+"</a>";
				}
				if(i==3){
					str += "...";
				}
				if(i > pageCount - 2){
					str += "<a href='javascript:showCategoryList("+i+")'>"+i+"</a>";
				}
			}
		}
	}
	return str;
}
	function paging() {
		$("#paging").empty();
		$("#paging").append("<a href='javascript:showCategoryList(1)' class='p_pre'>首页</a>");
		if (pageIndex == 1 && pageCount == 1) {
			$("#paging").append(pageNum());
		} else if (pageIndex == 1) {
			$("#paging").append(pageNum());
			$("#paging").append("<a href='javascript:showCategoryList(pageIndex+1)' class='p_pre'>下一页</a>");
		} else if (pageIndex == pageCount) {
			$("#paging").append("<a href='javascript:showCategoryList(pageIndex-1)' class='p_pre'>上一页</a>");
			$("#paging").append(pageNum());
		} else {
			$("#paging").append("<a href='javascript:showCategoryList(pageIndex-1)' class='p_pre'>上一页</a>");
			$("#paging").append(pageNum());
			$("#paging").append("<a href='javascript:showCategoryList(pageIndex+1)' class='p_pre'>下一页</a>");
		}
		$("#paging").append("<a href='javascript:showCategoryList(pageCount)' class='p_pre'>尾页</a>");
		bs();
	}
	function bs(){
		$("#paging a").each(function(){
		if(this.text==pageIndex){
			$(this).css({"background-color":"#FF4E00","color":"#fff"});
		}
	});
	$("#file").change(function(){
		getPath(document.getElementById("file"));
	});
	//读取图片本地完整路径
	function getPath(fileDom) {  
		//判断是否支持FileReader  
		if (window.FileReader) {  
   			var reader = new FileReader();  
		} else {  
   			alert("您的设备不支持图片预览功能，如需该功能请升级您的设备！");  
		}  
		//获取文件  
	    var file = fileDom.files[0];  
	    var imageType = /^image\//;  
	    //是否是图片  
	    if (!imageType.test(file.type)) {  
	        alert("仅支持图片！");  
	        return;  
	    }  
	    //读取完成  
	    reader.onload = function (e) {  
	        //获取图片dom  
	        var img = document.getElementById("img");  
	        //图片路径设置为读取的图片  
	        img.src = e.target.result;  
	    };  
	    reader.readAsDataURL(file);  
	}  
}
		
  		
</script>
  </head>
  <body>
  <%@ include file="topBar.jsp" %>
  <%@ include file="leftBar.jsp" %>
  <c:if test="${not empty param.mess}">
  	<script>alert("${param.mess}");</script>
  </c:if>
  <div id="productList">
    <p>分类列表
    <input type="hidden" id="pageIndex" name="pageIndex" value="${param.pageIndex }"/>
    <input type="hidden" id="pageCount" value="${param.pp }"/>
    </p>
    <input type="button" value="添加分类" id="btn" onclick="add()"/>
    <table id="pl">
    
    </table>
  </div>
  <div class="pages" id="paging">
  
  </div>
  <div id="addCategory">
	  <form action="category?opr=addCategory" enctype="multipart/form-data" method="post" id="add">
		  <p>添加分类</p> 
		    <table>
		    	<tr>
		    		<td>分类名称：<input type="text" name="name" id="name"/>
		    	</tr>
		    	<tr>
		    		<td>分类级别：
		    			<select name="type" id="type">
		    				<option value="1">一级菜单</option>
		    				<option value="2">二级菜单</option>
		    				<option value="3">三级菜单</option>
		    			</select>
					</td>
				</tr>
				<tr>
		    		<td>父级分类：
		    			<select name="parentType" id="parentType">
		    			
		    			</select>
		    		</td>   		
		    	</tr>
		    	<tr id="tu">
		    		<td style='text-indent: 1em'>图标：
		    		<p id="pimg"><img src="images/nav1.png" id="img"/></p>
					<input type="file" id="file" name="nfile" accept="image/*"/></td>
		    	</tr>
		    	<tr>
		    		<td><input type="submit" value="添加" id="addbtn"/><span id="span">×</span></td>
		    	</tr>
		    </table>
  		</form>
  </div>
  
  <%@ include file="bottomBar.jsp" %>
  </body>
</html>
